package com.zhaojonhy.zhaopangwb.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.net.RequestListener;
import com.sina.weibo.sdk.openapi.UsersAPI;
import com.zhaojonhy.zhaopangwb.ui.FriendsActivity;
import com.zhaojonhy.zhaopangwb.ui.SelfStatuseActivity;
import com.zhaojonhy.zhaopangwb.utils.AccessTokenKeeper;
import com.zhaojonhy.zhaopangwb.utils.BaseApplication;
import com.zhaojonhy.zhaopangwb.utils.Constants;
import com.zhaojonhy.zhaopangwb.R;
import com.zhaojonhy.zhaopangwb.adapter.UserItemAdapter;
import com.zhaojonhy.zhaopangwb.bean.User;
import com.zhaojonhy.zhaopangwb.bean.UserItem;
import com.zhaojonhy.zhaopangwb.ui.UserInfoActivity;
import com.zhaojonhy.zhaopangwb.utils.TitleBuilder;
import com.zhaojonhy.zhaopangwb.utils.ToastUtils;
import com.zhaojonhy.zhaopangwb.widget.WrapHeightListView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/9/5.
 */
public class UserFragment extends BaseFragment implements View.OnClickListener{
    final static String TAG = UserFragment.class.getSimpleName() ;

    private LinearLayout ll_userinfo ;

    private ImageView iv_avatar; //头像
    private TextView tv_subhead; //姓名
    private TextView tv_caption; //简介

    private LinearLayout ll_status_count ; //微博
    private LinearLayout ll_friends_count ; //关注
    private LinearLayout ll_followes_count ; //粉丝
    private TextView tv_status_count; //微博数
    private TextView tv_friends_count; //关注度
    private TextView tv_followes_count; //粉丝数

    private WrapHeightListView lv_user_items; //高度自适应的listView
    private ArrayList<UserItem> userItems ;
    private UserItemAdapter adapter ;

    private UsersAPI mUsersAPI;
    private Oauth2AccessToken mAccessToken;
    private User user ;
    private ImageLoader imageLoader ;

    private View view ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = View.inflate(activity, R.layout.frag_user,null) ;

        mAccessToken = AccessTokenKeeper.readAccessToken(activity) ;
        mUsersAPI = new UsersAPI(activity, Constants.APP_KEY, mAccessToken) ;
        imageLoader = ImageLoader.getInstance() ;

        initView() ;
        setItem() ;
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        // show/hide不会走生命周期
        Log.d(TAG,"onStart-->"+"user frag onStart()") ;
    }

    @Override
    public void onHiddenChanged(boolean hidden) { //监听隐藏变化
        super.onHiddenChanged(hidden);
        Log.d(TAG,"hidden-->"+2) ;
        //当true的时候隐藏，false的时候显示
        //当显示的时候加载用户的信息
        if( !hidden ){
            Log.d(TAG,"mAccessToken.getUid()-->"+mAccessToken.getUid());
            long uid = Long.valueOf(mAccessToken.getUid()).longValue();
            Log.d(TAG,"mAccessToken.getUid()-->"+uid);
            mUsersAPI.show(uid, new RequestListener() {
                @Override
                public void onComplete(String response) {
                    Log.d(TAG,"hidden-->response:"+response) ;
                    //数据获取成功后，将其保存在内存中或者其他地方，这里将其保存在application中
                    //application相当于单例
                    BaseApplication application = (BaseApplication) activity.getApplication() ;
                    Gson gson = new Gson() ;
                    application.currentUser = user = gson.fromJson(response,User.class) ;

                    //设置用户信息
                    setUserInfo() ;
                }

                @Override
                public void onWeiboException(WeiboException e) {
                    Log.d(TAG,"hidden-->response:"+e.getMessage()) ;

                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        Log.d(TAG,"v.getId():" + v.getId()) ;
        switch ( v.getId() ) {
            case R.id.ll_status_count :
                ToastUtils.showToast(activity,"我的微博", Toast.LENGTH_LONG);
                intent2Activity(SelfStatuseActivity.class);
                break;
            case R.id.ll_friends_count :
                ToastUtils.showToast(activity,"我的关注", Toast.LENGTH_LONG);
                intent2Activity(FriendsActivity.class);
                break;
            case R.id.ll_followes_count :
                ToastUtils.showToast(activity,"我的粉丝", Toast.LENGTH_LONG);
                intent2Activity(FriendsActivity.class,"isFollowers");
                break;
        }
    }

    private void setUserInfo() {
        tv_subhead.setText(user.getName());
        tv_caption.setText("简介:"+ (user.getDescription().equals("") ? "暂无介绍" :user.getDescription() ));
        Log.d(TAG,"user.getAvatar_large():"+user.getAvatar_large()) ;
        imageLoader.displayImage(user.getAvatar_large(),iv_avatar);
        tv_status_count.setText(""+user.getStatuses_count());
        tv_friends_count.setText(""+user.getFriends_count());
        tv_followes_count.setText(""+user.getFollowers_count());

        ll_status_count.setOnClickListener(this);
        ll_friends_count.setOnClickListener(this);
        ll_followes_count.setOnClickListener(this);
    }

    private void initView() {
        //标题栏
        new TitleBuilder(view)
                .setTitleText("我")
                .build();
        //用户信息
        ll_userinfo = (LinearLayout)view.findViewById(R.id.ll_userinfo) ;
        ll_userinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity,UserInfoActivity.class) ;
                startActivity(intent) ;
            }
        });

        iv_avatar = (ImageView) view.findViewById(R.id.iv_avatar) ;//头像
        tv_subhead = (TextView) view.findViewById(R.id.tv_subhead) ;//姓名
        tv_caption = (TextView) view.findViewById(R.id.tv_caption) ;//简介

        //互动信息栏: 微博、关注、粉丝linearLayout布局
        ll_status_count = (LinearLayout) view.findViewById(R.id.ll_status_count) ;
        ll_friends_count =  (LinearLayout) view.findViewById(R.id.ll_friends_count) ;
        ll_followes_count = (LinearLayout) view.findViewById(R.id.ll_followes_count) ;

        // 互动信息栏: 微博数、关注数、粉丝数
        tv_status_count = (TextView) view.findViewById(R.id.tv_status_count);
        tv_friends_count = (TextView) view.findViewById(R.id.tv_friends_count);
        tv_followes_count = (TextView) view.findViewById(R.id.tv_followes_count);
        // 设置栏列表
        lv_user_items = (WrapHeightListView) view.findViewById(R.id.lv_user_items);
        userItems = new ArrayList<UserItem>();
        Log.d(TAG,"userItems():"+userItems.size()) ;
        adapter = new UserItemAdapter(activity, userItems);
        lv_user_items.setAdapter(adapter);
    }

    private void setItem() {
        Log.d(TAG,"userItems() drawable:"+R.drawable.push_icon_app_small_1) ;
        userItems.add(new UserItem(false,R.drawable.push_icon_app_small_1,"新的朋友","")) ;
        userItems.add(new UserItem(true,R.drawable.push_icon_app_small_2,"微博等级","LV18")) ;
        userItems.add(new UserItem(false,R.drawable.push_icon_app_small_3,"我的相册","18")) ;
        userItems.add(new UserItem(false,R.drawable.push_icon_app_small_4,"我的点评","")) ;
        userItems.add(new UserItem(true,R.drawable.push_icon_app_small_5,"我的赞","32")) ;
        userItems.add(new UserItem(false,R.drawable.push_icon_app_small_6,"微博钱包","魅蓝之夜科技狂欢")) ;
        userItems.add(new UserItem(true,R.drawable.push_icon_app_small_7,"微博运动","每天10000步，你达标了吗？")) ;
        userItems.add(new UserItem(false,R.drawable.push_icon_app_small_8,"粉丝头条","推广博文及账号的利器")) ;
        userItems.add(new UserItem(true,R.drawable.push_icon_app_small_9,"粉丝服务","橱窗、私信、营销、数据助手")) ;
        userItems.add(new UserItem(true,R.drawable.push_icon_app_small_10,"草稿箱","")) ;
        userItems.add(new UserItem(false,R.drawable.push_icon_app_small_11,"更多","文章、收藏")) ;
        Log.d(TAG,"userItems():"+userItems.size()) ;
        adapter.notifyDataSetChanged();
    }

}
